// pages/login/login.js
let app = getApp();
var utils = require("../../utils/util")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadingHidden: true,
        tips: '',
        loginType: true,
        formData: {
            username: '',
            password: ''
        }
    },
    submitLogin: function () {
        let that = this
        if (that.data.formData.username.trim() && that.data.formData.password.trim()) {
            this.toLogin();
        } else {
            that.setTips('请完善用户名及密码')
        }
    },
    changeType() {
        let that = this
        utils.mask(that)
        this.setData({
            loginType: !this.data.loginType
        })
        utils.removeMask(that, 200)
    },
    setTips(msg) {
        this.setData({
            tips: msg
        })
    },
    toRegist: function () {
        utils.redirectToPage('/pages/register/register', this)
    },
    toIndex() {
        utils.redirectToPage('/pages/index/index', this)
    },
    toLogin: function () {
        let that = this
        let data = this.data.formData
        data.isStuLogin = this.data.loginType
        wx.request({
            url: app.globalData.serviceUrl + '/wx/login',
            type: 'post',
            data: data,
            success(res) {
                let resData = res.data
                if (resData.success) {
                    app.globalData.userType = data.isStuLogin ? 1 : 2
                    app.globalData.userInfo = resData.data
                    utils.toast('登录成功')
                    // utils.redirectToPage('/pages/index/index', that)
                    setTimeout(function () {
                        wx.switchTab({
                            url:'/pages/index/index'
                        });
                    }, 1000)
                } else {
                    that.setTips(resData.msg)
                }
            },
            fail() {
                that.setTips('请求失败')
            }
        })
    },

    /*********************以下为文本框监听***********************/
    watchUsername(e) {
        this.setData({
            ["formData.username"]: e.detail.value
        })
    },
    watchPassword(e) {
        this.setData({
            ["formData.password"]: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
